package dk.eal.kbr.mule.component.exchange;

import microsoft.exchange.webservices.data.Appointment;
import microsoft.exchange.webservices.data.DeleteMode;
import microsoft.exchange.webservices.data.ExchangeService;
import microsoft.exchange.webservices.data.ItemId;

public class DeleteCalenderItem extends ExchangeProxy {

	@Override
	public ExchangeCalenderItem doWork(ExchangeCalenderItem calenderItem) throws Exception {

		try {
			deleteAppointment(calenderItem);
			calenderItem.setIsActive(0);
			calenderItem.setDoDelete(1);

		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace(); // Test kode
			throw new Exception(e);
		}

		return calenderItem;
	}

	public void deleteAppointment(ExchangeCalenderItem externalData)
			throws Exception {
		Appointment appointment;

		ExchangeService service = connect(externalData.email,
				externalData.password);
		ItemId id = new ItemId(externalData.exchangeItemId);
		appointment = Appointment.bind(service, id);
		appointment.delete(DeleteMode.HardDelete);
	}

}
